// @ts-ignore
import baseJson from './base.json';
// @ts-ignore
import selectEventJson from './selectCardJson.json';
// @ts-ignore
import uploadCardJsonJson from './uploadCardJson.json';
// @ts-ignore
import extendFieldJson from './extendFieldJson.json';

// 办理获取formjson
export const getAllJson = (datas: any[]) => {
	const json = JSON.parse(JSON.stringify(baseJson));
	const newArr: any[] = [];
	datas.forEach((item: any) => {
		if (item.fieldEnglish !== 'executecont' && item.fieldEnglish !== 'attachment') {
			if ([1, 4, 5, 6, 7, 8].includes(item.fieldType)) {
				let validation: any = '';
				let validationHint = '';
				if (item.fieldType === 4) {
					validation = /^-?(0|[1-9][0-9]*)$/;
					validationHint = '请输入整数';
				} else if (item.fieldType === 8) {
					validation = /^(-?\d+)(\.\d+)?$/;
					validationHint = '请输入浮点数';
				}
				newArr.push({
					key: new Date().getTime(),
					type: 'input',
					alias: '',
					icon: 'text-field',
					formItemFlag: true,
					options: {
						name: item.fieldEnglish,
						keyNameEnabled: false,
						keyName: '',
						label: item.fieldName,
						labelAlign: 'label-left-align',
						type: 'text',
						defaultValue: '',
						placeholder: `请输入${item.fieldName}`,
						columnWidth: '200px',
						size: '',
						labelWidth: '110',
						labelHidden: false,
						labelWrap: false,
						maxHeight: 200,
						readonly: false,
						disabled: false,
						hidden: false,
						clearable: true,
						showPassword: false,
						required: item.isRequired,
						requiredHint: `请输入${item.fieldName}`,
						validation: validation,
						validationHint: validationHint,
						customClass: '',
						labelIconClass: null,
						labelIconPosition: 'rear',
						labelTooltip: null,
						minLength: null,
						maxLength: 50,
						showWordLimit: true,
						prefixIcon: '',
						suffixIcon: '',
						appendButton: false,
						appendButtonDisabled: false,
						buttonIcon: 'custom-search',
						appendText: false,
						textForAppend: '',
						onCreated: '',
						onMounted: '',
						onInput: '',
						onChange: '',
						onFocus: '',
						onBlur: '',
						onValidate: '',
						onAppendButtonClick: '',
					},
					id: `input${new Date().getTime()}`,
				});
			} else if ([2].includes(item.fieldType)) {
				let optionItems = [];
				if (item.pendingSelectionInformationList && item.pendingSelectionInformationList.length > 0) {
					optionItems = item.pendingSelectionInformationList.map((i: any) => {
						return {
							label: i.optionDescription,
							value: i.optionKey,
						};
					});
				}
				newArr.push({
					key: new Date().getTime(),
					type: 'select',
					icon: 'select-field',
					formItemFlag: true,
					options: {
						name: item.fieldEnglish,
						keyNameEnabled: false,
						keyName: '',
						label: item.fieldName,
						labelAlign: 'label-left-align',
						defaultValue: '',
						placeholder: `请选择${item.fieldName}`,
						columnWidth: '200px',
						size: '',
						labelWidth: '110',
						labelHidden: false,
						labelWrap: false,
						disabled: false,
						hidden: false,
						clearable: true,
						filterable: false,
						allowCreate: false,
						remote: false,
						automaticDropdown: false,
						multiple: item.isMultiSelect,
						multipleLimit: 0,
						collapseTags: false,
						dsEnabled: false,
						dsName: '',
						dataSetName: '',
						labelKey: 'label',
						valueKey: 'value',
						optionValueType: '',
						optionItems: optionItems,
						required: item.isRequired,
						requiredHint: `请选择${item.fieldName}`,
						validation: '',
						validationHint: '',
						customClass: '',
						labelIconClass: null,
						labelIconPosition: 'rear',
						labelTooltip: null,
						onCreated: '',
						onMounted: '',
						onRemoteQuery: '',
						onChange: '',
						onFocus: '',
						onBlur: '',
						onValidate: '',
					},
					id: `select${new Date().getTime()}`,
				});
			}
		}
	});
	json.widgetList = json.widgetList.concat(newArr);
	return json;
};
// 流程查看获取formjson
export const getViewAllJson = (datas: any[]) => {
	const json = JSON.parse(JSON.stringify(baseJson));
	const newArr: any[] = [];

	datas.forEach((item: any) => {
		if (item.fieldCode !== 'executecont' && item.fieldCode !== 'attachment') {
			if ([1, 4, 5, 6, 7, 8].includes(item.fieldType)) {
				let validation: any = '';
				let validationHint = '';
				newArr.push({
					key: new Date().getTime(),
					type: 'input',
					alias: '',
					icon: 'text-field',
					formItemFlag: true,
					options: {
						name: item.fieldCode,
						keyNameEnabled: false,
						keyName: '',
						label: item.fieldName,
						labelAlign: 'label-left-align',
						type: 'text',
						defaultValue: '',
						placeholder: `请输入${item.fieldName}`,
						columnWidth: '200px',
						size: '',
						labelWidth: '110',
						labelHidden: false,
						labelWrap: false,
						maxHeight: 200,
						readonly: false,
						disabled: false,
						hidden: false,
						clearable: true,
						showPassword: false,
						required: item.isRequired,
						requiredHint: `请输入${item.fieldName}`,
						validation: validation,
						validationHint: validationHint,
						customClass: '',
						labelIconClass: null,
						labelIconPosition: 'rear',
						labelTooltip: null,
						minLength: null,
						maxLength: 50,
						showWordLimit: true,
						prefixIcon: '',
						suffixIcon: '',
						appendButton: false,
						appendButtonDisabled: false,
						buttonIcon: 'custom-search',
						appendText: false,
						textForAppend: '',
						onCreated: '',
						onMounted: '',
						onInput: '',
						onChange: '',
						onFocus: '',
						onBlur: '',
						onValidate: '',
						onAppendButtonClick: '',
					},
					id: `input${new Date().getTime()}`,
				});
			} else if ([2].includes(item.fieldType)) {
				let optionItems = [];
				// 枚举
				if (item.fieldValue && item.fieldValue.length > 0) {
					let optionItemsArr = JSON.parse(item.fieldValue[0]);
					if (!Array.isArray(optionItemsArr)) {
						optionItemsArr = [optionItemsArr];
					}
					optionItems = optionItemsArr.map((i: any) => {
						return {
							label: i.optionDescription,
							value: i.optionKey,
						};
					});
				}
				newArr.push({
					key: new Date().getTime(),
					type: 'select',
					icon: 'select-field',
					formItemFlag: true,
					options: {
						name: item.fieldCode,
						keyNameEnabled: false,
						keyName: '',
						label: item.fieldName,
						labelAlign: 'label-left-align',
						defaultValue: '',
						placeholder: `请选择${item.fieldName}`,
						columnWidth: '200px',
						size: '',
						labelWidth: '110',
						labelHidden: false,
						labelWrap: false,
						disabled: false,
						hidden: false,
						clearable: true,
						filterable: false,
						allowCreate: false,
						remote: false,
						automaticDropdown: false,
						multiple: optionItems.length > 0 ? true : false,
						multipleLimit: 0,
						collapseTags: false,
						dsEnabled: false,
						dsName: '',
						dataSetName: '',
						labelKey: 'label',
						valueKey: 'value',
						optionValueType: '',
						optionItems: optionItems,
						required: item.isRequired,
						requiredHint: `请选择${item.fieldName}`,
						validation: '',
						validationHint: '',
						customClass: '',
						labelIconClass: null,
						labelIconPosition: 'rear',
						labelTooltip: null,
						onCreated: '',
						onMounted: '',
						onRemoteQuery: '',
						onChange: '',
						onFocus: '',
						onBlur: '',
						onValidate: '',
					},
					id: `select${new Date().getTime()}`,
				});
			}
		}
	});
	json.widgetList = json.widgetList.concat(newArr);
	return json;
};

export const getAllDetailJson = (formjson: any, isDetail = false, isEventJson = true) => {
	const json = JSON.parse(JSON.stringify(formjson));

	if (isEventJson) {
		json.widgetList.splice(0, 0, selectEventJson);
	}
	json.widgetList.push(uploadCardJsonJson);
	if (isDetail) {
		json.widgetList.push(extendFieldJson);
	}
	return json;
};
